package dao.impl;

import bean.Order;
import bean.User;
import com.mysql.cj.jdbc.ClientPreparedStatement;
import com.mysql.cj.jdbc.ConnectionImpl;
import com.sun.corba.se.impl.orb.ORBSingleton;
import dao.OrderDao;
import dao.OrderItemDao;
import jdk.nashorn.internal.runtime.OptimisticReturnFilters;
import service.impl.UserServiceImpl;
import util.DBCon;
import util.DataTypeConversion;

import java.sql.*;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.List;

public class OrderDaoImpl implements OrderDao {
    @Override
    public boolean deleteOrder(int orderId) {
        Connection con = DBCon.getConnection();
        if(con == null){
            System.out.println("数据库链接失败");
            return false;
        }
        try {
            PreparedStatement preparedStatement = con.prepareStatement("delete from table_order_item where order_id = ?");
            preparedStatement.setInt(1,orderId);
            int deleteNum = preparedStatement.executeUpdate();
            if(deleteNum > 0){
                System.out.println("你已经成功删除订单明细" + deleteNum + "条记录");
            }
             preparedStatement = con.prepareStatement("delete from table_order where order_id = ?");
            preparedStatement.setInt(1,orderId);
            deleteNum = preparedStatement.executeUpdate();
            if(deleteNum > 0){
                System.out.println("你已经成功删除订单" + deleteNum + "条记录");
                return true;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return false;
    }

    @Override
    public int addOrder(Order order) {
        Connection con = DBCon.getConnection();
        if(con == null){
            System.out.println("数据库链接失败");
            return 0;
        }
        try {
            PreparedStatement preparedStatement = con.prepareStatement("insert into table_order(u_id,screening_schedule_id,order_date,total)values(?,?,?,?)",Statement.RETURN_GENERATED_KEYS);
            preparedStatement.setInt(1,order.getUser().getId());
            preparedStatement.setInt(2,order.getScreeningSchedule().getId());
            preparedStatement.setTimestamp(3, Timestamp.valueOf(DataTypeConversion.conversionUtilDateToString(order.getOrderDate())));
            preparedStatement.setBigDecimal(4,order.getTotal());
            preparedStatement.executeUpdate();
            ResultSet rs = preparedStatement.getGeneratedKeys();
            if (rs.next()) {
                int orderId = rs.getInt(1);
                System.out.println("Generated order ID: " + orderId);
                return orderId;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
            return 0;
    }

    @Override
    public List<Order> getOrderListByUserId(int UserId) {
        Connection con = DBCon.getConnection();
        if(con == null){
            System.out.println("数据库链接失败");
            return null;
        }
        try {
            PreparedStatement preparedStatement = con.prepareStatement("select * from table_order where u_id = ?");
            preparedStatement.setInt(1,UserId);
            ResultSet rs = preparedStatement.executeQuery();
            List<Order> orderList = new ArrayList<>();
            while (rs.next()){
                Order order = new Order();
                order.setId(rs.getInt("order_id"));
                order.setUser(new UserDaoImpl().getUserByUserId(UserId));
                order.setTotal(rs.getBigDecimal("total"));
                order.setScreeningSchedule(new ScreeningScheduleDaoImpl().getScreeningScheduleById(rs.getInt("screening_schedule_id")));
                order.setOrderDate(rs.getTimestamp("order_date"));
                order.setOrderItemList(new OrderItemDaoImpl().getOrderItemsByOrderId(rs.getInt("order_id")));
                orderList.add(order);
            }
            return orderList;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Order> getOrderListByScreeningScheduleId(int ScreeningScheduleId) {
        Connection con = DBCon.getConnection();
        if(con == null){
            System.out.println("数据库连接失败");
            return null;
        }
        try {
            PreparedStatement pStmt= con.prepareStatement("select * from table_order where screening_schedule_id = ?");
            pStmt.setInt(1,ScreeningScheduleId);
            ResultSet rs =  pStmt.executeQuery();
            List<Order> orderList = new ArrayList<>();
            while (rs.next()){
                Order order = new Order();
                order.setId(rs.getInt(1));
                order.setOrderItemList(new OrderItemDaoImpl().getOrderItemsByOrderId(rs.getInt(1)));
                orderList.add(order);
            }
            return orderList;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    public boolean isSeatSold(int screeningScheduleId, String seat) {
        try {

            Connection con = DBCon.getConnection();
            if(con == null){
                System.out.println("数据库连接失败");
                return false;
            }
            //多表查询
            PreparedStatement pStmt = con.prepareStatement("SELECT * from table_order JOIN table_order_item  ON " +
                    " table_order.order_id = table_order_item.order_id " +
                    "where screening_schedule_id = ? and seat = ?");
            pStmt.setInt(1,screeningScheduleId);
            pStmt.setString(2,seat);
             ResultSet rs = pStmt.executeQuery();
             if (rs.next()){
                 System.out.println("该座位在table_order_item表中存在,即已经售卖");
                 return true;
             }
           return  false;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }

    @Override
    public Order getOrderById(int orderId) {
        Connection con = DBCon.getConnection();
        if(con == null){
            System.out.println("数据库连接失败");
            return null;
        }
        try {
            PreparedStatement pStmt = con.prepareStatement("select * from table_order where order_id = ?");
            pStmt.setInt(1,orderId);
            ResultSet rs = pStmt.executeQuery();
            if(rs.next()){
                Order order = new Order();
                order.setId(orderId);
                order.setUser(new UserDaoImpl().getUserByUserId(rs.getInt("u_id")));
                order.setOrderItemList(new OrderItemDaoImpl().getOrderItemsByOrderId(orderId));
                order.setTotal(rs.getBigDecimal("total"));
                order.setOrderDate(rs.getDate("order_date"));
                order.setScreeningSchedule(new ScreeningScheduleDaoImpl().getScreeningScheduleById(rs.getInt("screening_schedule_id")));
                return order;
            }
            return null;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }
}
